﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.PromptReport;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.EwesGiveBirthes
{
    public class ChildbirthTipQueryHandler : IRequestHandler<ChildbirthTipQueryCommand,ResultPage<ChildbirthTipQueryDto>>
    {
        private readonly IBaseRepository<PregnancyTest> _pregnancyTestRepository;
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;
        private readonly IBaseRepository<Breedingsetting> _breedingsettingRepository;

        public ChildbirthTipQueryHandler(IBaseRepository<PregnancyTest> pregnancyTestRepository, IBaseRepository<SheepArchives> sheepArchivesRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository, IBaseRepository<Breedingsetting> breedingsettingRepository)
        {
            _pregnancyTestRepository = pregnancyTestRepository;
            _sheepArchivesRepository = sheepArchivesRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
            _breedingsettingRepository = breedingsettingRepository;
        }
        /// <summary>
        /// 分娩配种业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<ChildbirthTipQueryDto>> Handle(ChildbirthTipQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<ChildbirthTipQueryDto>() { TotalCount=0,PagedCount=0};
            int day = 0;    
            try
            {
                var childbirthTip = await (from a in  await _pregnancyTestRepository.GetAll()
                                           join b in await _sheepArchivesRepository.GetAll()on a.EarBugles equals b.RecordAuricle
                                           join e in await _breedingsettingRepository.GetAll() on b.RecordAuricle equals e.RecordAuricle
                                           join c in await _cottageRepository.GetAll()  on a.CottageId equals c.CottageId
                                           join d in await _fieldRepository.GetAll() on a.FieldId equals d.FieldId
                                           where a.PregnancyTestResults=="成功" && b.RecordType==0
                                           select new ChildbirthTipQueryDto()
                                           {
                                               PregnancyTestId = a.PregnancyTestId,
                                               RecordAuricle = b.RecordAuricle,
                                               FieldId =d.FieldId,
                                               FieldName = d.FieldName,
                                               CottageId = c.CottageId,
                                               CottageName = c.CottageName,
                                               BreedingsettingTime = e.BreedingsettingTime,
                                               PregnancyDays = a.PregnancyDays,
                                           }).ToListAsync();
                result.TotalCount = childbirthTip.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                result.Data = childbirthTip.OrderBy(c=>c.PregnancyTestId).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
